package cn.tedu.knows.faq.aspecct;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Slf4j
public class PerformanceAspect {

    //定义切面,业务逻辑层所有方法
    @Pointcut("execution(" +
            "* cn.tedu.knows.faq.service.*Service.*(..))")
    public void time(){}

    @Around("time()")
    public Object performanceRecord(ProceedingJoinPoint joinPoint)
                                        throws Throwable {
        long start=System.currentTimeMillis();
        Object o=joinPoint.proceed();
        long end=System.currentTimeMillis();
        long time=end-start;
        //我们需要知道是哪个方法运行了这个时间,需要获得当前方法信息
        Signature method=joinPoint.getSignature();
        log.debug("{}方法运行时间:{}ms",method.getName(),time);
        return o;//别忘了返回
    }

}
